<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.14: http://docutils.sourceforge.net/" />
<title>Features.rst</title>
<style type="text/css">

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.

See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/

/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
  border: 0 }

table.borderless td, table.borderless th {
  /* Override padding for "table.docutils td" with "! important".
     The right padding separates the table cells. */
  padding: 0 0.5em 0 0 ! important }

.first {
  /* Override more specific margin styles with "! important". */
  margin-top: 0 ! important }

.last, .with-subtitle {
  margin-bottom: 0 ! important }

.hidden {
  display: none }

.subscript {
  vertical-align: sub;
  font-size: smaller }

.superscript {
  vertical-align: super;
  font-size: smaller }

a.toc-backref {
  text-decoration: none ;
  color: black }

blockquote.epigraph {
  margin: 2em 5em ; }

dl.docutils dd {
  margin-bottom: 0.5em }

object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
  overflow: hidden;
}

/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
  font-weight: bold }
*/

div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title, .code .error {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

/* Uncomment (and remove this text!) to get reduced vertical space in
   compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
  margin-bottom: 0.5em }

div.compound .compound-last, div.compound .compound-middle {
  margin-top: 0.5em }
*/

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em ;
  margin-right: 2em }

div.footer, div.header {
  clear: both;
  font-size: smaller }

div.line-block {
  display: block ;
  margin-top: 1em ;
  margin-bottom: 1em }

div.line-block div.line-block {
  margin-top: 0 ;
  margin-bottom: 0 ;
  margin-left: 1.5em }

div.sidebar {
  margin: 0 0 0.5em 1em ;
  border: medium outset ;
  padding: 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  margin-top: 0.4em }

h1.title {
  text-align: center }

h2.subtitle {
  text-align: center }

hr.docutils {
  width: 75% }

img.align-left, .figure.align-left, object.align-left, table.align-left {
  clear: left ;
  float: left ;
  margin-right: 1em }

img.align-right, .figure.align-right, object.align-right, table.align-right {
  clear: right ;
  float: right ;
  margin-left: 1em }

img.align-center, .figure.align-center, object.align-center {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

table.align-center {
  margin-left: auto;
  margin-right: auto;
}

.align-left {
  text-align: left }

.align-center {
  clear: both ;
  text-align: center }

.align-right {
  text-align: right }

/* reset inner alignment in figures */
div.align-right {
  text-align: inherit }

/* div.align-center * { */
/*   text-align: left } */

.align-top    {
  vertical-align: top }

.align-middle {
  vertical-align: middle }

.align-bottom {
  vertical-align: bottom }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font: inherit }

pre.literal-block, pre.doctest-block, pre.math, pre.code {
  margin-left: 2em ;
  margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
pre.code .literal.string, code .literal.string { color: #0C5404 }
pre.code .name.builtin, code .name.builtin { color: #352B84 }
pre.code .deleted, code .deleted { background-color: #DEB0A1}
pre.code .inserted, code .inserted { background-color: #A3D289}

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

span.section-subtitle {
  /* font-size relative to parent (h1..h6 element) */
  font-size: 80% }

table.citation {
  border-left: solid 1px gray;
  margin-left: 1px }

table.docinfo {
  margin: 2em 4em }

table.docutils {
  margin-top: 0.5em ;
  margin-bottom: 0.5em }

table.footnote {
  border-left: solid 1px black;
  margin-left: 1px }

table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

table.docutils th.field-name, table.docinfo th.docinfo-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap ;
  padding-left: 0 }

/* "booktabs" style (no vertical lines) */
table.docutils.booktabs {
  border: 0px;
  border-top: 2px solid;
  border-bottom: 2px solid;
  border-collapse: collapse;
}
table.docutils.booktabs * {
  border: 0px;
}
table.docutils.booktabs th {
  border-bottom: thin solid;
  text-align: left;
}

h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  font-size: 100% }

ul.auto-toc {
  list-style-type: none }

</style>
</head>
<body>
<div class="document">


<div class="section" id="introduction">
<h1>Introduction</h1>
<p>This regex implementation is backwards-compatible with the standard 're' module, but offers additional functionality.</p>
</div>
<div class="section" id="note">
<h1>Note</h1>
<p>The re module's behaviour with zero-width matches changed in Python 3.7, and this module will follow that behaviour when compiled for Python 3.7.</p>
</div>
<div class="section" id="old-vs-new-behaviour">
<h1>Old vs new behaviour</h1>
<p>In order to be compatible with the re module, this module has 2 behaviours:</p>
<ul>
<li><p class="first"><strong>Version 0</strong> behaviour (old behaviour, compatible with the re module):</p>
<p>Please note that the re module's behaviour may change over time, and I'll endeavour to match that behaviour in version 0.</p>
<ul class="simple">
<li>Indicated by the <tt class="docutils literal">VERSION0</tt> or <tt class="docutils literal">V0</tt> flag, or <tt class="docutils literal"><span class="pre">(?V0)</span></tt> in the pattern.</li>
<li>Zero-width matches are not handled correctly in the re module before Python 3.7. The behaviour in those earlier versions is:<ul>
<li><tt class="docutils literal">.split</tt> won't split a string at a zero-width match.</li>
<li><tt class="docutils literal">.sub</tt> will advance by one character after a zero-width match.</li>
</ul>
</li>
<li>Inline flags apply to the entire pattern, and they can't be turned off.</li>
<li>Only simple sets are supported.</li>
<li>Case-insensitive matches in Unicode use simple case-folding by default.</li>
</ul>
</li>
<li><p class="first"><strong>Version 1</strong> behaviour (new behaviour, possibly different from the re module):</p>
<ul class="simple">
<li>Indicated by the <tt class="docutils literal">VERSION1</tt> or <tt class="docutils literal">V1</tt> flag, or <tt class="docutils literal"><span class="pre">(?V1)</span></tt> in the pattern.</li>
<li>Zero-width matches are handled correctly.</li>
<li>Inline flags apply to the end of the group or pattern, and they can be turned off.</li>
<li>Nested sets and set operations are supported.</li>
<li>Case-insensitive matches in Unicode use full case-folding by default.</li>
</ul>
</li>
</ul>
<p>If no version is specified, the regex module will default to <tt class="docutils literal">regex.DEFAULT_VERSION</tt>.</p>
</div>
<div class="section" id="case-insensitive-matches-in-unicode">
<h1>Case-insensitive matches in Unicode</h1>
<p>The regex module supports both simple and full case-folding for case-insensitive matches in Unicode. Use of full case-folding can be turned on using the <tt class="docutils literal">FULLCASE</tt> or <tt class="docutils literal">F</tt> flag, or <tt class="docutils literal"><span class="pre">(?f)</span></tt> in the pattern. Please note that this flag affects how the <tt class="docutils literal">IGNORECASE</tt> flag works; the <tt class="docutils literal">FULLCASE</tt> flag itself does not turn on case-insensitive matching.</p>
<p>In the version 0 behaviour, the flag is off by default.</p>
<p>In the version 1 behaviour, the flag is on by default.</p>
</div>
<div class="section" id="nested-sets-and-set-operations">
<h1>Nested sets and set operations</h1>
<p>It's not possible to support both simple sets, as used in the re module, and nested sets at the same time because of a difference in the meaning of an unescaped <tt class="docutils literal">&quot;[&quot;</tt> in a set.</p>
<p>For example, the pattern <tt class="docutils literal"><span class="pre">[[a-z]--[aeiou]]</span></tt> is treated in the version 0 behaviour (simple sets, compatible with the re module) as:</p>
<ul class="simple">
<li>Set containing &quot;[&quot; and the letters &quot;a&quot; to &quot;z&quot;</li>
<li>Literal &quot;--&quot;</li>
<li>Set containing letters &quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;</li>
<li>Literal &quot;]&quot;</li>
</ul>
<p>but in the version 1 behaviour (nested sets, enhanced behaviour) as:</p>
<ul class="simple">
<li>Set which is:<ul>
<li>Set containing the letters &quot;a&quot; to &quot;z&quot;</li>
</ul>
</li>
<li>but excluding:<ul>
<li>Set containing the letters &quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;</li>
</ul>
</li>
</ul>
<p>Version 0 behaviour: only simple sets are supported.</p>
<p>Version 1 behaviour: nested sets and set operations are supported.</p>
</div>
<div class="section" id="flags">
<h1>Flags</h1>
<p>There are 2 kinds of flag: scoped and global. Scoped flags can apply to only part of a pattern and can be turned on or off; global flags apply to the entire pattern and can only be turned on.</p>
<p>The scoped flags are: <tt class="docutils literal">FULLCASE</tt>, <tt class="docutils literal">IGNORECASE</tt>, <tt class="docutils literal">MULTILINE</tt>, <tt class="docutils literal">DOTALL</tt>, <tt class="docutils literal">VERBOSE</tt>, <tt class="docutils literal">WORD</tt>.</p>
<p>The global flags are: <tt class="docutils literal">ASCII</tt>, <tt class="docutils literal">BESTMATCH</tt>, <tt class="docutils literal">ENHANCEMATCH</tt>, <tt class="docutils literal">LOCALE</tt>, <tt class="docutils literal">POSIX</tt>, <tt class="docutils literal">REVERSE</tt>, <tt class="docutils literal">UNICODE</tt>, <tt class="docutils literal">VERSION0</tt>, <tt class="docutils literal">VERSION1</tt>.</p>
<p>If neither the <tt class="docutils literal">ASCII</tt>, <tt class="docutils literal">LOCALE</tt> nor <tt class="docutils literal">UNICODE</tt> flag is specified, it will default to <tt class="docutils literal">UNICODE</tt> if the regex pattern is a Unicode string and <tt class="docutils literal">ASCII</tt> if it's a bytestring.</p>
<p>The <tt class="docutils literal">ENHANCEMATCH</tt> flag makes fuzzy matching attempt to improve the fit of the next match that it finds.</p>
<p>The <tt class="docutils literal">BESTMATCH</tt> flag makes fuzzy matching search for the best match instead of the next match.</p>
</div>
<div class="section" id="notes-on-named-capture-groups">
<h1>Notes on named capture groups</h1>
<p>All capture groups have a group number, starting from 1.</p>
<p>Groups with the same group name will have the same group number, and groups with a different group name will have a different group number.</p>
<p>The same name can be used by more than one group, with later captures 'overwriting' earlier captures. All of the captures of the group will be available from the <tt class="docutils literal">captures</tt> method of the match object.</p>
<p>Group numbers will be reused across different branches of a branch reset, eg. <tt class="docutils literal"><span class="pre">(?|(first)|(second))</span></tt> has only group 1. If capture groups have different group names then they will, of course, have different group numbers, eg. <tt class="docutils literal"><span class="pre">(?|(?P&lt;foo&gt;first)|(?P&lt;bar&gt;second))</span></tt> has group 1 (&quot;foo&quot;) and group 2 (&quot;bar&quot;).</p>
<p>In the regex <tt class="docutils literal"><span class="pre">(\s+)(?|(?P&lt;foo&gt;[A-Z]+)|(\w+)</span> <span class="pre">(?P&lt;foo&gt;[0-9]+)</span></tt> there are 2 groups:</p>
<ul class="simple">
<li><tt class="docutils literal">(\s+)</tt> is group 1.</li>
<li><tt class="docutils literal"><span class="pre">(?P&lt;foo&gt;[A-Z]+)</span></tt> is group 2, also called &quot;foo&quot;.</li>
<li><tt class="docutils literal">(\w+)</tt> is group 2 because of the branch reset.</li>
<li><tt class="docutils literal"><span class="pre">(?P&lt;foo&gt;[0-9]+)</span></tt> is group 2 because it's called &quot;foo&quot;.</li>
</ul>
<p>If you want to prevent <tt class="docutils literal">(\w+)</tt> from being group 2, you need to name it (different name, different group number).</p>
</div>
<div class="section" id="multithreading">
<h1>Multithreading</h1>
<p>The regex module releases the GIL during matching on instances of the built-in (immutable) string classes, enabling other Python threads to run concurrently. It is also possible to force the regex module to release the GIL during matching by calling the matching methods with the keyword argument <tt class="docutils literal">concurrent=True</tt>. The behaviour is undefined if the string changes during matching, so use it <em>only</em> when it is guaranteed that that won't happen.</p>
</div>
<div class="section" id="unicode">
<h1>Unicode</h1>
<p>This module supports Unicode 12.1.0.</p>
<p>Full Unicode case-folding is supported.</p>
</div>
<div class="section" id="additional-features">
<h1>Additional features</h1>
<p>The issue numbers relate to the Python bug tracker, except where listed as &quot;Hg issue&quot;.</p>
<ul>
<li><p class="first">Added support for lookaround in conditional pattern (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/163">Hg issue 163</a>)</p>
<p>The test of a conditional pattern can now be a lookaround.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'(?(?=\d)\d+|\w+)'</span><span class="punctuation">,</span> <span class="literal string single">'123abc'</span><span class="punctuation">)</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">3</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'123'</span><span class="operator">&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'(?(?=\d)\d+|\w+)'</span><span class="punctuation">,</span> <span class="literal string single">'abc123'</span><span class="punctuation">)</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">6</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'abc123'</span><span class="operator">&gt;</span>
</pre>
<p>This is not quite the same as putting a lookaround in the first branch of a pair of alternatives.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'(?:(?=\d)\d+\b|\w+)'</span><span class="punctuation">,</span> <span class="literal string single">'123abc'</span><span class="punctuation">))</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">6</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'123abc'</span><span class="operator">&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'(?(?=\d)\d+\b|\w+)'</span><span class="punctuation">,</span> <span class="literal string single">'123abc'</span><span class="punctuation">))</span>
<span class="name builtin pseudo">None</span>
</pre>
<p>In the first example, the lookaround matched, but the remainder of the first branch failed to match, and so the second branch was attempted, whereas in the second example, the lookaround matched, and the first branch failed to match, but the second branch was <strong>not</strong> attempted.</p>
</li>
<li><p class="first">Added POSIX matching (leftmost longest) (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/150">Hg issue 150</a>)</p>
<p>The POSIX standard for regex is to return the leftmost longest match. This can be turned on using the <tt class="docutils literal">POSIX</tt> flag (<tt class="docutils literal"><span class="pre">(?p)</span></tt>).</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># Normal matching.</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'Mr|Mrs'</span><span class="punctuation">,</span> <span class="literal string single">'Mrs'</span><span class="punctuation">)</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">2</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'Mr'</span><span class="operator">&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'one(self)?(selfsufficient)?'</span><span class="punctuation">,</span> <span class="literal string single">'oneselfsufficient'</span><span class="punctuation">)</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">7</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'oneself'</span><span class="operator">&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># POSIX matching.</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'(?p)Mr|Mrs'</span><span class="punctuation">,</span> <span class="literal string single">'Mrs'</span><span class="punctuation">)</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">3</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'Mrs'</span><span class="operator">&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'(?p)one(self)?(selfsufficient)?'</span><span class="punctuation">,</span> <span class="literal string single">'oneselfsufficient'</span><span class="punctuation">)</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">17</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'oneselfsufficient'</span><span class="operator">&gt;</span>
</pre>
<p>Note that it will take longer to find matches because when it finds a match at a certain position, it won't return that immediately, but will keep looking to see if there's another longer match there.</p>
</li>
<li><p class="first">Added <tt class="docutils literal"><span class="pre">(?(DEFINE)...)</span></tt> (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/152">Hg issue 152</a>)</p>
<p>If there's no group called &quot;DEFINE&quot;, then ... will be ignored, but any group definitions within it will be available.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'(?(DEFINE)(?P&lt;quant&gt;\d+)(?P&lt;item&gt;\w+))(?&amp;quant) (?&amp;item)'</span><span class="punctuation">,</span> <span class="literal string single">'5 elephants'</span><span class="punctuation">)</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">11</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'5 elephants'</span><span class="operator">&gt;</span>
</pre>
</li>
<li><p class="first">Added <tt class="docutils literal">(*PRUNE)</tt>, <tt class="docutils literal">(*SKIP)</tt> and <tt class="docutils literal">(*FAIL)</tt> (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/153">Hg issue 153</a>)</p>
<p><tt class="docutils literal">(*PRUNE)</tt> discards the backtracking info up to that point. When used in an atomic group or a lookaround, it won't affect the enclosing pattern.</p>
<p><tt class="docutils literal">(*SKIP)</tt> is similar to <tt class="docutils literal">(*PRUNE)</tt>, except that it also sets where in the text the next attempt to match will start. When used in an atomic group or a lookaround, it won't affect the enclosing pattern.</p>
<p><tt class="docutils literal">(*FAIL)</tt> causes immediate backtracking. <tt class="docutils literal">(*F)</tt> is a permitted abbreviation.</p>
</li>
<li><p class="first">Added <tt class="docutils literal">\K</tt> (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/151">Hg issue 151</a>)</p>
<p>Keeps the part of the entire match after the position where <tt class="docutils literal">\K</tt> occurred; the part before it is discarded.</p>
<p>It does not affect what capture groups return.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'(\w\w\K\w\w\w)'</span><span class="punctuation">,</span> <span class="literal string single">'abcdef'</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="punctuation">[</span><span class="literal number integer">0</span><span class="punctuation">]</span>
<span class="literal string single">'cde'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">]</span>
<span class="literal string single">'abcde'</span>
<span class="operator">&gt;&gt;&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'(?r)(\w\w\K\w\w\w)'</span><span class="punctuation">,</span> <span class="literal string single">'abcdef'</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="punctuation">[</span><span class="literal number integer">0</span><span class="punctuation">]</span>
<span class="literal string single">'bc'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">]</span>
<span class="literal string single">'bcdef'</span>
</pre>
</li>
<li><p class="first">Added capture subscripting for <tt class="docutils literal">expandf</tt> and <tt class="docutils literal">subf</tt>/<tt class="docutils literal">subfn</tt> (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/133">Hg issue 133</a>)</p>
<p>You can now use subscripting to get the captures of a repeated capture group.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(\w)+&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abc&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">expandf</span><span class="punctuation">(</span><span class="literal string double">&quot;{1}&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'c'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">expandf</span><span class="punctuation">(</span><span class="literal string double">&quot;{1[0]} {1[1]} {1[2]}&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'a b c'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">expandf</span><span class="punctuation">(</span><span class="literal string double">&quot;{1[-1]} {1[-2]} {1[-3]}&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'c b a'</span>
<span class="operator">&gt;&gt;&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;letter&gt;\w)+&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abc&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">expandf</span><span class="punctuation">(</span><span class="literal string double">&quot;{letter}&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'c'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">expandf</span><span class="punctuation">(</span><span class="literal string double">&quot;{letter[0]} {letter[1]} {letter[2]}&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'a b c'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">expandf</span><span class="punctuation">(</span><span class="literal string double">&quot;{letter[-1]} {letter[-2]} {letter[-3]}&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'c b a'</span>
</pre>
</li>
<li><p class="first">Added support for referring to a group by number using <tt class="docutils literal"><span class="pre">(?P=...)</span></tt>.</p>
<p>This is in addition to the existing <tt class="docutils literal"><span class="pre">\g&lt;...&gt;</span></tt>.</p>
</li>
<li><p class="first">Fixed the handling of locale-sensitive regexes.</p>
<p>The <tt class="docutils literal">LOCALE</tt> flag is intended for legacy code and has limited support. You're still recommended to use Unicode instead.</p>
</li>
<li><p class="first">Added partial matches (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/102">Hg issue 102</a>)</p>
<p>A partial match is one that matches up to the end of string, but that string has been truncated and you want to know whether a complete match could be possible if the string had not been truncated.</p>
<p>Partial matches are supported by <tt class="docutils literal">match</tt>, <tt class="docutils literal">search</tt>, <tt class="docutils literal">fullmatch</tt> and <tt class="docutils literal">finditer</tt> with the <tt class="docutils literal">partial</tt> keyword argument.</p>
<p>Match objects have a <tt class="docutils literal">partial</tt> attribute, which is <tt class="docutils literal">True</tt> if it's a partial match.</p>
<p>For example, if you wanted a user to enter a 4-digit number and check it character by character as it was being entered:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">pattern</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">compile</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'\d{4}'</span><span class="punctuation">)</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># Initially, nothing has been entered:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">pattern</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string single">''</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">))</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">0</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">''</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="operator">&gt;</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># An empty string is OK, but it's only a partial match.</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># The user enters a letter:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">pattern</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string single">'a'</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">))</span>
<span class="name builtin pseudo">None</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># It'll never match.</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># The user deletes that and enters a digit:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">pattern</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string single">'1'</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">))</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'1'</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="operator">&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># It matches this far, but it's only a partial match.</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># The user enters 2 more digits:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">pattern</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string single">'123'</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">))</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">3</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'123'</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="operator">&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># It matches this far, but it's only a partial match.</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># The user enters another digit:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">pattern</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string single">'1234'</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">))</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">4</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'1234'</span><span class="operator">&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># It's a complete match.</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># If the user enters another digit:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">pattern</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string single">'12345'</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">))</span>
<span class="name builtin pseudo">None</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># It's no longer a match.</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># This is a partial match:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">pattern</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string single">'123'</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">partial</span>
<span class="name builtin pseudo">True</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># This is a complete match:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">pattern</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string single">'1233'</span><span class="punctuation">,</span> <span class="name">partial</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">partial</span>
<span class="name builtin pseudo">False</span>
</pre>
</li>
<li><p class="first"><tt class="docutils literal">*</tt> operator not working correctly with sub() (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/106">Hg issue 106</a>)</p>
<p>Sometimes it's not clear how zero-width matches should be handled. For example, should <tt class="docutils literal">.*</tt> match 0 characters directly after matching &gt;0 characters?</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="comment single"># Python 3.7 and later</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">sub</span><span class="punctuation">(</span><span class="literal string single">'.*'</span><span class="punctuation">,</span> <span class="literal string single">'x'</span><span class="punctuation">,</span> <span class="literal string single">'test'</span><span class="punctuation">)</span>
<span class="literal string single">'xx'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">sub</span><span class="punctuation">(</span><span class="literal string single">'.*?'</span><span class="punctuation">,</span> <span class="literal string single">'|'</span><span class="punctuation">,</span> <span class="literal string single">'test'</span><span class="punctuation">)</span>
<span class="literal string single">'|||||||||'</span>

<span class="comment single"># Python 3.6 and earlier</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">sub</span><span class="punctuation">(</span><span class="literal string single">'(?V0).*'</span><span class="punctuation">,</span> <span class="literal string single">'x'</span><span class="punctuation">,</span> <span class="literal string single">'test'</span><span class="punctuation">)</span>
<span class="literal string single">'x'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">sub</span><span class="punctuation">(</span><span class="literal string single">'(?V1).*'</span><span class="punctuation">,</span> <span class="literal string single">'x'</span><span class="punctuation">,</span> <span class="literal string single">'test'</span><span class="punctuation">)</span>
<span class="literal string single">'xx'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">sub</span><span class="punctuation">(</span><span class="literal string single">'(?V0).*?'</span><span class="punctuation">,</span> <span class="literal string single">'|'</span><span class="punctuation">,</span> <span class="literal string single">'test'</span><span class="punctuation">)</span>
<span class="literal string single">'|t|e|s|t|'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">sub</span><span class="punctuation">(</span><span class="literal string single">'(?V1).*?'</span><span class="punctuation">,</span> <span class="literal string single">'|'</span><span class="punctuation">,</span> <span class="literal string single">'test'</span><span class="punctuation">)</span>
<span class="literal string single">'|||||||||'</span>
</pre>
</li>
<li><p class="first">Added <tt class="docutils literal">capturesdict</tt> (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/86">Hg issue 86</a>)</p>
<p><tt class="docutils literal">capturesdict</tt> is a combination of <tt class="docutils literal">groupdict</tt> and <tt class="docutils literal">captures</tt>:</p>
<p><tt class="docutils literal">groupdict</tt> returns a dict of the named groups and the last capture of those groups.</p>
<p><tt class="docutils literal">captures</tt> returns a list of all the captures of a group</p>
<p><tt class="docutils literal">capturesdict</tt> returns a dict of the named groups and lists of all the captures of those groups.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?:(?P&lt;word&gt;\w+) (?P&lt;digits&gt;\d+)\n)+&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;one 1</span><span class="literal string escape">\n</span><span class="literal string double">two 2</span><span class="literal string escape">\n</span><span class="literal string double">three 3</span><span class="literal string escape">\n</span><span class="literal string double">&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">groupdict</span><span class="punctuation">()</span>
<span class="punctuation">{</span><span class="literal string single">'word'</span><span class="punctuation">:</span> <span class="literal string single">'three'</span><span class="punctuation">,</span> <span class="literal string single">'digits'</span><span class="punctuation">:</span> <span class="literal string single">'3'</span><span class="punctuation">}</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">captures</span><span class="punctuation">(</span><span class="literal string double">&quot;word&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'one'</span><span class="punctuation">,</span> <span class="literal string single">'two'</span><span class="punctuation">,</span> <span class="literal string single">'three'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">captures</span><span class="punctuation">(</span><span class="literal string double">&quot;digits&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'1'</span><span class="punctuation">,</span> <span class="literal string single">'2'</span><span class="punctuation">,</span> <span class="literal string single">'3'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">capturesdict</span><span class="punctuation">()</span>
<span class="punctuation">{</span><span class="literal string single">'word'</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="literal string single">'one'</span><span class="punctuation">,</span> <span class="literal string single">'two'</span><span class="punctuation">,</span> <span class="literal string single">'three'</span><span class="punctuation">],</span> <span class="literal string single">'digits'</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="literal string single">'1'</span><span class="punctuation">,</span> <span class="literal string single">'2'</span><span class="punctuation">,</span> <span class="literal string single">'3'</span><span class="punctuation">]}</span>
</pre>
</li>
<li><p class="first">Allow duplicate names of groups (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/87">Hg issue 87</a>)</p>
<p>Group names can now be duplicated.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># With optional groups:</span>
<span class="operator">&gt;&gt;&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># Both groups capture, the second capture 'overwriting' the first.</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;item&gt;\w+)? or (?P&lt;item&gt;\w+)?&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;first or second&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'second'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">captures</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'first'</span><span class="punctuation">,</span> <span class="literal string single">'second'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># Only the second group captures.</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;item&gt;\w+)? or (?P&lt;item&gt;\w+)?&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot; or second&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'second'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">captures</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'second'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># Only the first group captures.</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;item&gt;\w+)? or (?P&lt;item&gt;\w+)?&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;first or &quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'first'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">captures</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'first'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># With mandatory groups:</span>
<span class="operator">&gt;&gt;&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># Both groups capture, the second capture 'overwriting' the first.</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;item&gt;\w*) or (?P&lt;item&gt;\w*)?&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;first or second&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'second'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">captures</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'first'</span><span class="punctuation">,</span> <span class="literal string single">'second'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># Again, both groups capture, the second capture 'overwriting' the first.</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;item&gt;\w*) or (?P&lt;item&gt;\w*)&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot; or second&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'second'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">captures</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">''</span><span class="punctuation">,</span> <span class="literal string single">'second'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># And yet again, both groups capture, the second capture 'overwriting' the first.</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;item&gt;\w*) or (?P&lt;item&gt;\w*)&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;first or &quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="literal string single">''</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">captures</span><span class="punctuation">(</span><span class="literal string double">&quot;item&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'first'</span><span class="punctuation">,</span> <span class="literal string single">''</span><span class="punctuation">]</span>
</pre>
</li>
<li><p class="first">Added <tt class="docutils literal">fullmatch</tt> (<a class="reference external" href="https://bugs.python.org/issue16203">issue #16203</a>)</p>
<p><tt class="docutils literal">fullmatch</tt> behaves like <tt class="docutils literal">match</tt>, except that it must match all of the string.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">regex</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;abc&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abc&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">span</span><span class="punctuation">())</span>
<span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">3</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">regex</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;abc&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abcx&quot;</span><span class="punctuation">))</span>
<span class="name builtin pseudo">None</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">regex</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;abc&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abcx&quot;</span><span class="punctuation">,</span> <span class="name">endpos</span><span class="operator">=</span><span class="literal number integer">3</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">span</span><span class="punctuation">())</span>
<span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">3</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">regex</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;abc&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;xabcy&quot;</span><span class="punctuation">,</span> <span class="name">pos</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">,</span> <span class="name">endpos</span><span class="operator">=</span><span class="literal number integer">4</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">span</span><span class="punctuation">())</span>
<span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">,</span> <span class="literal number integer">4</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;a.*?&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abcd&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">)</span>
<span class="literal string single">'a'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;a.*?&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abcd&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">)</span>
<span class="literal string single">'abcd'</span>
</pre>
</li>
<li><p class="first">Added <tt class="docutils literal">subf</tt> and <tt class="docutils literal">subfn</tt></p>
<p><tt class="docutils literal">subf</tt> and <tt class="docutils literal">subfn</tt> are alternatives to <tt class="docutils literal">sub</tt> and <tt class="docutils literal">subn</tt> respectively. When passed a replacement string, they treat it as a format string.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">subf</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(\w+) (\w+)&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;{0} =&gt; {2} {1}&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;foo bar&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'foo bar =&gt; bar foo'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">subf</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;word1&gt;\w+) (?P&lt;word2&gt;\w+)&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;{word2} {word1}&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;foo bar&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'bar foo'</span>
</pre>
</li>
<li><p class="first">Added <tt class="docutils literal">expandf</tt> to match object</p>
<p><tt class="docutils literal">expandf</tt> is an alternative to <tt class="docutils literal">expand</tt>. When passed a replacement string, it treats it as a format string.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(\w+) (\w+)&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;foo bar&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">expandf</span><span class="punctuation">(</span><span class="literal string double">&quot;{0} =&gt; {2} {1}&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'foo bar =&gt; bar foo'</span>
<span class="operator">&gt;&gt;&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;word1&gt;\w+) (?P&lt;word2&gt;\w+)&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;foo bar&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">expandf</span><span class="punctuation">(</span><span class="literal string double">&quot;{word2} {word1}&quot;</span><span class="punctuation">)</span>
<span class="literal string single">'bar foo'</span>
</pre>
</li>
<li><p class="first">Detach searched string</p>
<p>A match object contains a reference to the string that was searched, via its <tt class="docutils literal">string</tt> attribute. The <tt class="docutils literal">detach_string</tt> method will 'detach' that string, making it available for garbage collection, which might save valuable memory if that string is very large.</p>
<p>Example:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;\w+&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;Hello world&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">())</span>
<span class="name">Hello</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">m</span><span class="operator">.</span><span class="name">string</span><span class="punctuation">)</span>
<span class="name">Hello</span> <span class="name">world</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">detach_string</span><span class="punctuation">()</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">())</span>
<span class="name">Hello</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">m</span><span class="operator">.</span><span class="name">string</span><span class="punctuation">)</span>
<span class="name builtin pseudo">None</span>
</pre>
</li>
<li><p class="first">Recursive patterns (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/27">Hg issue 27</a>)</p>
<p>Recursive and repeated patterns are supported.</p>
<p><tt class="docutils literal"><span class="pre">(?R)</span></tt> or <tt class="docutils literal"><span class="pre">(?0)</span></tt> tries to match the entire regex recursively. <tt class="docutils literal"><span class="pre">(?1)</span></tt>, <tt class="docutils literal"><span class="pre">(?2)</span></tt>, etc, try to match the relevant capture group.</p>
<p><tt class="docutils literal"><span class="pre">(?&amp;name)</span></tt> tries to match the named capture group.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(Tarzan|Jane) loves (?1)&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;Tarzan loves Jane&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">groups</span><span class="punctuation">()</span>
<span class="punctuation">(</span><span class="literal string single">'Tarzan'</span><span class="punctuation">,)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(Tarzan|Jane) loves (?1)&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;Jane loves Tarzan&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">groups</span><span class="punctuation">()</span>
<span class="punctuation">(</span><span class="literal string single">'Jane'</span><span class="punctuation">,)</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(\w)(?:(?R)|(\w?))\1&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;kayak&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">,</span> <span class="literal number integer">2</span><span class="punctuation">)</span>
<span class="punctuation">(</span><span class="literal string single">'kayak'</span><span class="punctuation">,</span> <span class="literal string single">'k'</span><span class="punctuation">,</span> <span class="name builtin pseudo">None</span><span class="punctuation">)</span>
</pre>
<p>The first two examples show how the subpattern within the capture group is reused, but is _not_ itself a capture group. In other words, <tt class="docutils literal">&quot;(Tarzan|Jane) loves <span class="pre">(?1)&quot;</span></tt> is equivalent to <tt class="docutils literal">&quot;(Tarzan|Jane) loves <span class="pre">(?:Tarzan|Jane)&quot;</span></tt>.</p>
<p>It's possible to backtrack into a recursed or repeated group.</p>
<p>You can't call a group if there is more than one group with that group name or group number (<tt class="docutils literal">&quot;ambiguous group reference&quot;</tt>).</p>
<p>The alternative forms <tt class="docutils literal"><span class="pre">(?P&gt;name)</span></tt> and <tt class="docutils literal"><span class="pre">(?P&amp;name)</span></tt> are also supported.</p>
</li>
<li><p class="first">Full Unicode case-folding is supported.</p>
<p>In version 1 behaviour, the regex module uses full case-folding when performing case-insensitive matches in Unicode.</p>
<p>Examples (in Python 3):</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?iV1)strasse&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;stra</span><span class="literal string escape">\N{LATIN SMALL LETTER SHARP S}</span><span class="literal string double">e&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">span</span><span class="punctuation">()</span>
<span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">6</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?iV1)stra\N{LATIN SMALL LETTER SHARP S}e&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;STRASSE&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">span</span><span class="punctuation">()</span>
<span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">7</span><span class="punctuation">)</span>
</pre>
<p>In version 0 behaviour, it uses simple case-folding for backward compatibility with the re module.</p>
</li>
<li><p class="first">Approximate &quot;fuzzy&quot; matching (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/12">Hg issue 12</a>, <a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/41">Hg issue 41</a>, <a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/109">Hg issue 109</a>)</p>
<p>Regex usually attempts an exact match, but sometimes an approximate, or &quot;fuzzy&quot;, match is needed, for those cases where the text being searched may contain errors in the form of inserted, deleted or substituted characters.</p>
<p>A fuzzy regex specifies which types of errors are permitted, and, optionally, either the minimum and maximum or only the maximum permitted number of each type. (You cannot specify only a minimum.)</p>
<p>The 3 types of error are:</p>
<ul class="simple">
<li>Insertion, indicated by &quot;i&quot;</li>
<li>Deletion, indicated by &quot;d&quot;</li>
<li>Substitution, indicated by &quot;s&quot;</li>
</ul>
<p>In addition, &quot;e&quot; indicates any type of error.</p>
<p>The fuzziness of a regex item is specified between &quot;{&quot; and &quot;}&quot; after the item.</p>
<p>Examples:</p>
<ul class="simple">
<li><tt class="docutils literal">foo</tt> match &quot;foo&quot; exactly</li>
<li><tt class="docutils literal"><span class="pre">(?:foo){i}</span></tt> match &quot;foo&quot;, permitting insertions</li>
<li><tt class="docutils literal"><span class="pre">(?:foo){d}</span></tt> match &quot;foo&quot;, permitting deletions</li>
<li><tt class="docutils literal"><span class="pre">(?:foo){s}</span></tt> match &quot;foo&quot;, permitting substitutions</li>
<li><tt class="docutils literal"><span class="pre">(?:foo){i,s}</span></tt> match &quot;foo&quot;, permitting insertions and substitutions</li>
<li><tt class="docutils literal"><span class="pre">(?:foo){e}</span></tt> match &quot;foo&quot;, permitting errors</li>
</ul>
<p>If a certain type of error is specified, then any type not specified will <strong>not</strong> be permitted.</p>
<p>In the following examples I'll omit the item and write only the fuzziness:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">{d&lt;=3}</span></tt> permit at most 3 deletions, but no other types</li>
<li><tt class="docutils literal"><span class="pre">{i&lt;=1,s&lt;=2}</span></tt> permit at most 1 insertion and at most 2 substitutions, but no deletions</li>
<li><tt class="docutils literal"><span class="pre">{1&lt;=e&lt;=3}</span></tt> permit at least 1 and at most 3 errors</li>
<li><tt class="docutils literal"><span class="pre">{i&lt;=2,d&lt;=2,e&lt;=3}</span></tt> permit at most 2 insertions, at most 2 deletions, at most 3 errors in total, but no substitutions</li>
</ul>
<p>It's also possible to state the costs of each type of error and the maximum permitted total cost.</p>
<p>Examples:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">{2i+2d+1s&lt;=4}</span></tt> each insertion costs 2, each deletion costs 2, each substitution costs 1, the total cost must not exceed 4</li>
<li><tt class="docutils literal"><span class="pre">{i&lt;=1,d&lt;=1,s&lt;=1,2i+2d+1s&lt;=4}</span></tt> at most 1 insertion, at most 1 deletion, at most 1 substitution; each insertion costs 2, each deletion costs 2, each substitution costs 1, the total cost must not exceed 4</li>
</ul>
<p>You can also use &quot;&lt;&quot; instead of &quot;&lt;=&quot; if you want an exclusive minimum or maximum.</p>
<p>By default, fuzzy matching searches for the first match that meets the given constraints. The <tt class="docutils literal">ENHANCEMATCH</tt> flag will cause it to attempt to improve the fit (i.e. reduce the number of errors) of the match that it has found.</p>
<p>The <tt class="docutils literal">BESTMATCH</tt> flag will make it search for the best match instead.</p>
<p>Further examples to note:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">regex.search(&quot;(dog){e}&quot;,</span> &quot;cat and <span class="pre">dog&quot;)[1]</span></tt> returns <tt class="docutils literal">&quot;cat&quot;</tt> because that matches <tt class="docutils literal">&quot;dog&quot;</tt> with 3 errors (an unlimited number of errors is permitted).</li>
<li><tt class="docutils literal"><span class="pre">regex.search(&quot;(dog){e&lt;=1}&quot;,</span> &quot;cat and <span class="pre">dog&quot;)[1]</span></tt> returns <tt class="docutils literal">&quot; dog&quot;</tt> (with a leading space) because that matches <tt class="docutils literal">&quot;dog&quot;</tt> with 1 error, which is within the limit.</li>
<li><tt class="docutils literal"><span class="pre">regex.search(&quot;(?e)(dog){e&lt;=1}&quot;,</span> &quot;cat and <span class="pre">dog&quot;)[1]</span></tt> returns <tt class="docutils literal">&quot;dog&quot;</tt> (without a leading space) because the fuzzy search matches <tt class="docutils literal">&quot; dog&quot;</tt> with 1 error, which is within the limit, and the <tt class="docutils literal"><span class="pre">(?e)</span></tt> then it attempts a better fit.</li>
</ul>
<p>In the first two examples there are perfect matches later in the string, but in neither case is it the first possible match.</p>
<p>The match object has an attribute <tt class="docutils literal">fuzzy_counts</tt> which gives the total number of substitutions, insertions and deletions.</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># A 'raw' fuzzy match:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?:cats|cat){e&lt;=1}&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;cat&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">fuzzy_counts</span>
<span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># 0 substitutions, 0 insertions, 1 deletion.</span>

<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># A better match might be possible if the ENHANCEMATCH flag used:</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">fullmatch</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?e)(?:cats|cat){e&lt;=1}&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;cat&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">fuzzy_counts</span>
<span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">0</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="comment single"># 0 substitutions, 0 insertions, 0 deletions.</span>
</pre>
<p>The match object also has an attribute <tt class="docutils literal">fuzzy_changes</tt> which gives a tuple of the positions of the substitutions, insertions and deletions.</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string single">'(fuu){i&lt;=2,d&lt;=2,e&lt;=5}'</span><span class="punctuation">,</span> <span class="literal string single">'anaconda foo bar'</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span>
<span class="operator">&lt;</span><span class="name">regex</span><span class="operator">.</span><span class="name">Match</span> <span class="name builtin">object</span><span class="punctuation">;</span> <span class="name">span</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">7</span><span class="punctuation">,</span> <span class="literal number integer">10</span><span class="punctuation">),</span> <span class="name">match</span><span class="operator">=</span><span class="literal string single">'a f'</span><span class="punctuation">,</span> <span class="name">fuzzy_counts</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">2</span><span class="punctuation">,</span> <span class="literal number integer">2</span><span class="punctuation">)</span><span class="operator">&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">fuzzy_changes</span>
<span class="punctuation">([],</span> <span class="punctuation">[</span><span class="literal number integer">7</span><span class="punctuation">,</span> <span class="literal number integer">8</span><span class="punctuation">],</span> <span class="punctuation">[</span><span class="literal number integer">10</span><span class="punctuation">,</span> <span class="literal number integer">11</span><span class="punctuation">])</span>
</pre>
<p>What this means is that if the matched part of the string had been:</p>
<pre class="code python literal-block">
<span class="literal string single">'anacondfuuoo bar'</span>
</pre>
<p>it would've been an exact match.</p>
<p>However, there were insertions at positions 7 and 8:</p>
<pre class="code python literal-block">
<span class="literal string single">'anaconda fuuoo bar'</span>
        <span class="operator">^^</span>
</pre>
<p>and deletions at positions 10 and 11:</p>
<pre class="code python literal-block">
<span class="literal string single">'anaconda f~~oo bar'</span>
           <span class="operator">^^</span>
</pre>
<p>So the actual string was:</p>
<pre class="code python literal-block">
<span class="literal string single">'anaconda foo bar'</span>
</pre>
</li>
<li><p class="first">Named lists (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/11">Hg issue 11</a>)</p>
<p><tt class="docutils literal">\L&lt;name&gt;</tt></p>
<p>There are occasions where you may want to include a list (actually, a set) of options in a regex.</p>
<p>One way is to build the pattern like this:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">p</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">compile</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;first|second|third|fourth|fifth&quot;</span><span class="punctuation">)</span>
</pre>
<p>but if the list is large, parsing the resulting regex can take considerable time, and care must also be taken that the strings are properly escaped and properly ordered, for example, &quot;cats&quot; before &quot;cat&quot;.</p>
<p>The new alternative is to use a named list:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">option_set</span> <span class="operator">=</span> <span class="punctuation">[</span><span class="literal string double">&quot;first&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;second&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;third&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;fourth&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;fifth&quot;</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">p</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">compile</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;\L&lt;options&gt;&quot;</span><span class="punctuation">,</span> <span class="name">options</span><span class="operator">=</span><span class="name">option_set</span><span class="punctuation">)</span>
</pre>
<p>The order of the items is irrelevant, they are treated as a set. The named lists are available as the <tt class="docutils literal">.named_lists</tt> attribute of the pattern object :</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">p</span><span class="operator">.</span><span class="name">named_lists</span><span class="punctuation">)</span>
<span class="comment single"># Python 3</span>
<span class="punctuation">{</span><span class="literal string single">'options'</span><span class="punctuation">:</span> <span class="name builtin">frozenset</span><span class="punctuation">({</span><span class="literal string single">'fifth'</span><span class="punctuation">,</span> <span class="literal string single">'first'</span><span class="punctuation">,</span> <span class="literal string single">'fourth'</span><span class="punctuation">,</span> <span class="literal string single">'second'</span><span class="punctuation">,</span> <span class="literal string single">'third'</span><span class="punctuation">})}</span>
<span class="comment single"># Python 2</span>
<span class="punctuation">{</span><span class="literal string single">'options'</span><span class="punctuation">:</span> <span class="name builtin">frozenset</span><span class="punctuation">([</span><span class="literal string single">'fifth'</span><span class="punctuation">,</span> <span class="literal string single">'fourth'</span><span class="punctuation">,</span> <span class="literal string single">'second'</span><span class="punctuation">,</span> <span class="literal string single">'third'</span><span class="punctuation">,</span> <span class="literal string single">'first'</span><span class="punctuation">])}</span>
</pre>
</li>
<li><p class="first">Start and end of word</p>
<p><tt class="docutils literal">\m</tt> matches at the start of a word.</p>
<p><tt class="docutils literal">\M</tt> matches at the end of a word.</p>
<p>Compare with <tt class="docutils literal">\b</tt>, which matches at the start or end of a word.</p>
</li>
<li><p class="first">Unicode line separators</p>
<p>Normally the only line separator is <tt class="docutils literal">\n</tt> (<tt class="docutils literal">\x0A</tt>), but if the <tt class="docutils literal">WORD</tt> flag is turned on then the line separators are <tt class="docutils literal">\x0D\x0A</tt>, <tt class="docutils literal">\x0A</tt>, <tt class="docutils literal">\x0B</tt>, <tt class="docutils literal">\x0C</tt> and <tt class="docutils literal">\x0D</tt>, plus <tt class="docutils literal">\x85</tt>, <tt class="docutils literal">\u2028</tt> and <tt class="docutils literal">\u2029</tt> when working with Unicode.</p>
<p>This affects the regex dot <tt class="docutils literal">&quot;.&quot;</tt>, which, with the <tt class="docutils literal">DOTALL</tt> flag turned off, matches any character except a line separator. It also affects the line anchors <tt class="docutils literal">^</tt> and <tt class="docutils literal">$</tt> (in multiline mode).</p>
</li>
<li><p class="first">Set operators</p>
<p><strong>Version 1 behaviour only</strong></p>
<p>Set operators have been added, and a set <tt class="docutils literal"><span class="pre">[...]</span></tt> can include nested sets.</p>
<p>The operators, in order of increasing precedence, are:</p>
<ul class="simple">
<li><tt class="docutils literal">||</tt> for union (&quot;x||y&quot; means &quot;x or y&quot;)</li>
<li><tt class="docutils literal">~~</tt> (double tilde) for symmetric difference (&quot;x~~y&quot; means &quot;x or y, but not both&quot;)</li>
<li><tt class="docutils literal">&amp;&amp;</tt> for intersection (&quot;x&amp;&amp;y&quot; means &quot;x and y&quot;)</li>
<li><tt class="docutils literal"><span class="pre">--</span></tt> (double dash) for difference (&quot;x--y&quot; means &quot;x but not y&quot;)</li>
</ul>
<p>Implicit union, ie, simple juxtaposition like in <tt class="docutils literal">[ab]</tt>, has the highest precedence. Thus, <tt class="docutils literal"><span class="pre">[ab&amp;&amp;cd]</span></tt> is the same as <tt class="docutils literal"><span class="pre">[[a||b]&amp;&amp;[c||d]]</span></tt>.</p>
<p>Examples:</p>
<ul class="simple">
<li><tt class="docutils literal">[ab]</tt> # Set containing 'a' and 'b'</li>
<li><tt class="docutils literal"><span class="pre">[a-z]</span></tt> # Set containing 'a' .. 'z'</li>
<li><tt class="docutils literal"><span class="pre">[[a-z]--[qw]]</span></tt> # Set containing 'a' .. 'z', but not 'q' or 'w'</li>
<li><tt class="docutils literal"><span class="pre">[a-z--qw]</span></tt> # Same as above</li>
<li><tt class="docutils literal"><span class="pre">[\p{L}--QW]</span></tt> # Set containing all letters except 'Q' and 'W'</li>
<li><tt class="docutils literal"><span class="pre">[\p{N}--[0-9]]</span></tt> # Set containing all numbers except '0' .. '9'</li>
<li><tt class="docutils literal"><span class="pre">[\p{ASCII}&amp;&amp;\p{Letter}]</span></tt> # Set containing all characters which are ASCII and letter</li>
</ul>
</li>
<li><p class="first">regex.escape (<a class="reference external" href="https://bugs.python.org/issue2650">issue #2650</a>)</p>
<p>regex.escape has an additional keyword parameter <tt class="docutils literal">special_only</tt>. When True, only 'special' regex characters, such as '?', are escaped.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">escape</span><span class="punctuation">(</span><span class="literal string double">&quot;foo!?&quot;</span><span class="punctuation">,</span> <span class="name">special_only</span><span class="operator">=</span><span class="name builtin pseudo">False</span><span class="punctuation">)</span>
<span class="literal string single">'foo</span><span class="literal string escape">\\</span><span class="literal string single">!</span><span class="literal string escape">\\</span><span class="literal string single">?'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">escape</span><span class="punctuation">(</span><span class="literal string double">&quot;foo!?&quot;</span><span class="punctuation">,</span> <span class="name">special_only</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">)</span>
<span class="literal string single">'foo!</span><span class="literal string escape">\\</span><span class="literal string single">?'</span>
</pre>
</li>
<li><p class="first">regex.escape (<a class="reference external" href="https://bitbucket.org/mrabarnett/mrab-regex/issues/249">Hg issue 249</a>)</p>
<p>regex.escape has an additional keyword parameter <tt class="docutils literal">literal_spaces</tt>. When True, spaces are not escaped.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">escape</span><span class="punctuation">(</span><span class="literal string double">&quot;foo bar!?&quot;</span><span class="punctuation">,</span> <span class="name">literal_spaces</span><span class="operator">=</span><span class="name builtin pseudo">False</span><span class="punctuation">)</span>
<span class="literal string single">'foo</span><span class="literal string escape">\\</span><span class="literal string single"> bar!</span><span class="literal string escape">\\</span><span class="literal string single">?'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">escape</span><span class="punctuation">(</span><span class="literal string double">&quot;foo bar!?&quot;</span><span class="punctuation">,</span> <span class="name">literal_spaces</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">)</span>
<span class="literal string single">'foo bar!</span><span class="literal string escape">\\</span><span class="literal string single">?'</span>
</pre>
</li>
<li><p class="first">Repeated captures (<a class="reference external" href="https://bugs.python.org/issue7132">issue #7132</a>)</p>
<p>A match object has additional methods which return information on all the successful matches of a repeated capture group. These methods are:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">matchobject.captures([group1,</span> <span class="pre">...])</span></tt><ul>
<li>Returns a list of the strings matched in a group or groups. Compare with <tt class="docutils literal"><span class="pre">matchobject.group([group1,</span> <span class="pre">...])</span></tt>.</li>
</ul>
</li>
<li><tt class="docutils literal"><span class="pre">matchobject.starts([group])</span></tt><ul>
<li>Returns a list of the start positions. Compare with <tt class="docutils literal"><span class="pre">matchobject.start([group])</span></tt>.</li>
</ul>
</li>
<li><tt class="docutils literal"><span class="pre">matchobject.ends([group])</span></tt><ul>
<li>Returns a list of the end positions. Compare with <tt class="docutils literal"><span class="pre">matchobject.end([group])</span></tt>.</li>
</ul>
</li>
<li><tt class="docutils literal"><span class="pre">matchobject.spans([group])</span></tt><ul>
<li>Returns a list of the spans. Compare with <tt class="docutils literal"><span class="pre">matchobject.span([group])</span></tt>.</li>
</ul>
</li>
</ul>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(\w{3})+&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;123456789&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">group</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="literal string single">'789'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">captures</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'123'</span><span class="punctuation">,</span> <span class="literal string single">'456'</span><span class="punctuation">,</span> <span class="literal string single">'789'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">start</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="literal number integer">6</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">starts</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">3</span><span class="punctuation">,</span> <span class="literal number integer">6</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">end</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="literal number integer">9</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">ends</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal number integer">3</span><span class="punctuation">,</span> <span class="literal number integer">6</span><span class="punctuation">,</span> <span class="literal number integer">9</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">span</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="punctuation">(</span><span class="literal number integer">6</span><span class="punctuation">,</span> <span class="literal number integer">9</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span><span class="operator">.</span><span class="name">spans</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="punctuation">[(</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">3</span><span class="punctuation">),</span> <span class="punctuation">(</span><span class="literal number integer">3</span><span class="punctuation">,</span> <span class="literal number integer">6</span><span class="punctuation">),</span> <span class="punctuation">(</span><span class="literal number integer">6</span><span class="punctuation">,</span> <span class="literal number integer">9</span><span class="punctuation">)]</span>
</pre>
</li>
<li><p class="first">Atomic grouping (<a class="reference external" href="https://bugs.python.org/issue433030">issue #433030</a>)</p>
<p><tt class="docutils literal"><span class="pre">(?&gt;...)</span></tt></p>
<p>If the following pattern subsequently fails, then the subpattern as a whole will fail.</p>
</li>
<li><p class="first">Possessive quantifiers.</p>
<p><tt class="docutils literal"><span class="pre">(?:...)?+</span></tt> ; <tt class="docutils literal"><span class="pre">(?:...)*+</span></tt> ; <tt class="docutils literal"><span class="pre">(?:...)++</span></tt> ; <tt class="docutils literal"><span class="pre">(?:...){min,max}+</span></tt></p>
<p>The subpattern is matched up to 'max' times. If the following pattern subsequently fails, then all of the repeated subpatterns will fail as a whole. For example, <tt class="docutils literal"><span class="pre">(?:...)++</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(?&gt;(?:...)+)</span></tt>.</p>
</li>
<li><p class="first">Scoped flags (<a class="reference external" href="https://bugs.python.org/issue433028">issue #433028</a>)</p>
<p><tt class="docutils literal"><span class="pre">(?flags-flags:...)</span></tt></p>
<p>The flags will apply only to the subpattern. Flags can be turned on or off.</p>
</li>
<li><p class="first">Definition of 'word' character (<a class="reference external" href="https://bugs.python.org/issue1693050">issue #1693050</a>)</p>
<p>The definition of a 'word' character has been expanded for Unicode. It now conforms to the Unicode specification at <tt class="docutils literal"><span class="pre">http://www.unicode.org/reports/tr29/</span></tt>.</p>
</li>
<li><p class="first">Variable-length lookbehind</p>
<p>A lookbehind can match a variable-length string.</p>
</li>
<li><p class="first">Flags argument for regex.split, regex.sub and regex.subn (<a class="reference external" href="https://bugs.python.org/issue3482">issue #3482</a>)</p>
<p><tt class="docutils literal">regex.split</tt>, <tt class="docutils literal">regex.sub</tt> and <tt class="docutils literal">regex.subn</tt> support a 'flags' argument.</p>
</li>
<li><p class="first">Pos and endpos arguments for regex.sub and regex.subn</p>
<p><tt class="docutils literal">regex.sub</tt> and <tt class="docutils literal">regex.subn</tt> support 'pos' and 'endpos' arguments.</p>
</li>
<li><p class="first">'Overlapped' argument for regex.findall and regex.finditer</p>
<p><tt class="docutils literal">regex.findall</tt> and <tt class="docutils literal">regex.finditer</tt> support an 'overlapped' flag which permits overlapped matches.</p>
</li>
<li><p class="first">Splititer</p>
<p><tt class="docutils literal">regex.splititer</tt> has been added. It's a generator equivalent of <tt class="docutils literal">regex.split</tt>.</p>
</li>
<li><p class="first">Subscripting for groups</p>
<p>A match object accepts access to the captured groups via subscripting and slicing:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">m</span> <span class="operator">=</span> <span class="name">regex</span><span class="operator">.</span><span class="name">search</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?P&lt;before&gt;.*?)(?P&lt;num&gt;\d+)(?P&lt;after&gt;.*)&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;pqr123stu&quot;</span><span class="punctuation">)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">m</span><span class="punctuation">[</span><span class="literal string double">&quot;before&quot;</span><span class="punctuation">])</span>
<span class="name">pqr</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">m</span><span class="punctuation">))</span>
<span class="literal number integer">4</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">print</span><span class="punctuation">(</span><span class="name">m</span><span class="punctuation">[:])</span>
<span class="punctuation">(</span><span class="literal string single">'pqr123stu'</span><span class="punctuation">,</span> <span class="literal string single">'pqr'</span><span class="punctuation">,</span> <span class="literal string single">'123'</span><span class="punctuation">,</span> <span class="literal string single">'stu'</span><span class="punctuation">)</span>
</pre>
</li>
<li><p class="first">Named groups</p>
<p>Groups can be named with <tt class="docutils literal"><span class="pre">(?&lt;name&gt;...)</span></tt> as well as the current <tt class="docutils literal"><span class="pre">(?P&lt;name&gt;...)</span></tt>.</p>
</li>
<li><p class="first">Group references</p>
<p>Groups can be referenced within a pattern with <tt class="docutils literal">\g&lt;name&gt;</tt>. This also allows there to be more than 99 groups.</p>
</li>
<li><p class="first">Named characters</p>
<p><tt class="docutils literal">\N{name}</tt></p>
<p>Named characters are supported. (Note: only those known by Python's Unicode database are supported.)</p>
</li>
<li><p class="first">Unicode codepoint properties, including scripts and blocks</p>
<p><tt class="docutils literal">\p{property=value}</tt>; <tt class="docutils literal">\P{property=value}</tt>; <tt class="docutils literal">\p{value}</tt> ; <tt class="docutils literal">\P{value}</tt></p>
<p>Many Unicode properties are supported, including blocks and scripts. <tt class="docutils literal">\p{property=value}</tt> or <tt class="docutils literal">\p{property:value}</tt> matches a character whose property <tt class="docutils literal">property</tt> has value <tt class="docutils literal">value</tt>. The inverse of <tt class="docutils literal">\p{property=value}</tt> is <tt class="docutils literal">\P{property=value}</tt> or <tt class="docutils literal"><span class="pre">\p{^property=value}</span></tt>.</p>
<p>If the short form <tt class="docutils literal">\p{value}</tt> is used, the properties are checked in the order: <tt class="docutils literal">General_Category</tt>, <tt class="docutils literal">Script</tt>, <tt class="docutils literal">Block</tt>, binary property:</p>
<ul class="simple">
<li><tt class="docutils literal">Latin</tt>, the 'Latin' script (<tt class="docutils literal">Script=Latin</tt>).</li>
<li><tt class="docutils literal">BasicLatin</tt>, the 'BasicLatin' block (<tt class="docutils literal">Block=BasicLatin</tt>).</li>
<li><tt class="docutils literal">Alphabetic</tt>, the 'Alphabetic' binary property (<tt class="docutils literal">Alphabetic=Yes</tt>).</li>
</ul>
<p>A short form starting with <tt class="docutils literal">Is</tt> indicates a script or binary property:</p>
<ul class="simple">
<li><tt class="docutils literal">IsLatin</tt>, the 'Latin' script (<tt class="docutils literal">Script=Latin</tt>).</li>
<li><tt class="docutils literal">IsAlphabetic</tt>, the 'Alphabetic' binary property (<tt class="docutils literal">Alphabetic=Yes</tt>).</li>
</ul>
<p>A short form starting with <tt class="docutils literal">In</tt> indicates a block property:</p>
<ul class="simple">
<li><tt class="docutils literal">InBasicLatin</tt>, the 'BasicLatin' block (<tt class="docutils literal">Block=BasicLatin</tt>).</li>
</ul>
</li>
<li><p class="first">POSIX character classes</p>
<p><tt class="docutils literal"><span class="pre">[[:alpha:]]</span></tt>; <tt class="docutils literal"><span class="pre">[[:^alpha:]]</span></tt></p>
<p>POSIX character classes are supported. These are normally treated as an alternative form of <tt class="docutils literal"><span class="pre">\p{...}</span></tt>.</p>
<p>The exceptions are <tt class="docutils literal">alnum</tt>, <tt class="docutils literal">digit</tt>, <tt class="docutils literal">punct</tt> and <tt class="docutils literal">xdigit</tt>, whose definitions are different from those of Unicode.</p>
<p><tt class="docutils literal"><span class="pre">[[:alnum:]]</span></tt> is equivalent to <tt class="docutils literal">\p{posix_alnum}</tt>.</p>
<p><tt class="docutils literal"><span class="pre">[[:digit:]]</span></tt> is equivalent to <tt class="docutils literal">\p{posix_digit}</tt>.</p>
<p><tt class="docutils literal"><span class="pre">[[:punct:]]</span></tt> is equivalent to <tt class="docutils literal">\p{posix_punct}</tt>.</p>
<p><tt class="docutils literal"><span class="pre">[[:xdigit:]]</span></tt> is equivalent to <tt class="docutils literal">\p{posix_xdigit}</tt>.</p>
</li>
<li><p class="first">Search anchor</p>
<p><tt class="docutils literal">\G</tt></p>
<p>A search anchor has been added. It matches at the position where each search started/continued and can be used for contiguous matches or in negative variable-length lookbehinds to limit how far back the lookbehind goes:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">findall</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;\w{2}&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abcd ef&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'ab'</span><span class="punctuation">,</span> <span class="literal string single">'cd'</span><span class="punctuation">,</span> <span class="literal string single">'ef'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">findall</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;\G\w{2}&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abcd ef&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'ab'</span><span class="punctuation">,</span> <span class="literal string single">'cd'</span><span class="punctuation">]</span>
</pre>
<ul class="simple">
<li>The search starts at position 0 and matches 2 letters 'ab'.</li>
<li>The search continues at position 2 and matches 2 letters 'cd'.</li>
<li>The search continues at position 4 and fails to match any letters.</li>
<li>The anchor stops the search start position from being advanced, so there are no more results.</li>
</ul>
</li>
<li><p class="first">Reverse searching</p>
<p>Searches can now work backwards:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">findall</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;.&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abc&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'a'</span><span class="punctuation">,</span> <span class="literal string single">'b'</span><span class="punctuation">,</span> <span class="literal string single">'c'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">findall</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?r).&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abc&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'c'</span><span class="punctuation">,</span> <span class="literal string single">'b'</span><span class="punctuation">,</span> <span class="literal string single">'a'</span><span class="punctuation">]</span>
</pre>
<p>Note: the result of a reverse search is not necessarily the reverse of a forward search:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">findall</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;..&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abcde&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'ab'</span><span class="punctuation">,</span> <span class="literal string single">'cd'</span><span class="punctuation">]</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">findall</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?r)..&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;abcde&quot;</span><span class="punctuation">)</span>
<span class="punctuation">[</span><span class="literal string single">'de'</span><span class="punctuation">,</span> <span class="literal string single">'bc'</span><span class="punctuation">]</span>
</pre>
</li>
<li><p class="first">Matching a single grapheme</p>
<p><tt class="docutils literal">\X</tt></p>
<p>The grapheme matcher is supported. It now conforms to the Unicode specification at <tt class="docutils literal"><span class="pre">http://www.unicode.org/reports/tr29/</span></tt>.</p>
</li>
<li><p class="first">Branch reset</p>
<p><tt class="docutils literal"><span class="pre">(?|...|...)</span></tt></p>
<p>Capture group numbers will be reused across the alternatives, but groups with different names will have different group numbers.</p>
<p>Examples:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?|(first)|(second))&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;first&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">groups</span><span class="punctuation">()</span>
<span class="punctuation">(</span><span class="literal string single">'first'</span><span class="punctuation">,)</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">match</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string double">&quot;(?|(first)|(second))&quot;</span><span class="punctuation">,</span> <span class="literal string double">&quot;second&quot;</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">groups</span><span class="punctuation">()</span>
<span class="punctuation">(</span><span class="literal string single">'second'</span><span class="punctuation">,)</span>
</pre>
<p>Note that there is only one group.</p>
</li>
<li><p class="first">Default Unicode word boundary</p>
<p>The <tt class="docutils literal">WORD</tt> flag changes the definition of a 'word boundary' to that of a default Unicode word boundary. This applies to <tt class="docutils literal">\b</tt> and <tt class="docutils literal">\B</tt>.</p>
</li>
<li><p class="first">Timeout (Python 3)</p>
<p>The matching methods and functions support timeouts. The timeout (in seconds) applies to the entire operation:</p>
<pre class="code python literal-block">
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword namespace">from</span> <span class="name namespace">time</span> <span class="keyword namespace">import</span> <span class="name">sleep</span>
<span class="operator">&gt;&gt;&gt;</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">def</span> <span class="name function">fast_replace</span><span class="punctuation">(</span><span class="name">m</span><span class="punctuation">):</span>
<span class="operator">...</span>     <span class="keyword">return</span> <span class="literal string single">'X'</span>
<span class="operator">...</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="keyword">def</span> <span class="name function">slow_replace</span><span class="punctuation">(</span><span class="name">m</span><span class="punctuation">):</span>
<span class="operator">...</span>     <span class="name">sleep</span><span class="punctuation">(</span><span class="literal number float">0.5</span><span class="punctuation">)</span>
<span class="operator">...</span>     <span class="keyword">return</span> <span class="literal string single">'X'</span>
<span class="operator">...</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">sub</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'[a-z]'</span><span class="punctuation">,</span> <span class="name">fast_replace</span><span class="punctuation">,</span> <span class="literal string single">'abcde'</span><span class="punctuation">,</span> <span class="name">timeout</span><span class="operator">=</span><span class="literal number integer">2</span><span class="punctuation">)</span>
<span class="literal string single">'XXXXX'</span>
<span class="operator">&gt;&gt;&gt;</span> <span class="name">regex</span><span class="operator">.</span><span class="name">sub</span><span class="punctuation">(</span><span class="literal string affix">r</span><span class="literal string single">'[a-z]'</span><span class="punctuation">,</span> <span class="name">slow_replace</span><span class="punctuation">,</span> <span class="literal string single">'abcde'</span><span class="punctuation">,</span> <span class="name">timeout</span><span class="operator">=</span><span class="literal number integer">2</span><span class="punctuation">)</span>
<span class="name">Traceback</span> <span class="punctuation">(</span><span class="name">most</span> <span class="name">recent</span> <span class="name">call</span> <span class="name">last</span><span class="punctuation">):</span>
  <span class="name">File</span> <span class="literal string double">&quot;&lt;stdin&gt;&quot;</span><span class="punctuation">,</span> <span class="name">line</span> <span class="literal number integer">1</span><span class="punctuation">,</span> <span class="operator word">in</span> <span class="operator">&lt;</span><span class="name">module</span><span class="operator">&gt;</span>
  <span class="name">File</span> <span class="literal string double">&quot;C:\Python37\lib\site-packages</span><span class="literal string escape">\r</span><span class="literal string double">egex</span><span class="literal string escape">\r</span><span class="literal string double">egex.py&quot;</span><span class="punctuation">,</span> <span class="name">line</span> <span class="literal number integer">276</span><span class="punctuation">,</span> <span class="operator word">in</span> <span class="name">sub</span>
    <span class="name">endpos</span><span class="punctuation">,</span> <span class="name">concurrent</span><span class="punctuation">,</span> <span class="name">timeout</span><span class="punctuation">)</span>
<span class="name">TimeoutError</span><span class="punctuation">:</span> <span class="name">regex</span> <span class="name">timed</span> <span class="name">out</span>
</pre>
</li>
</ul>
</div>
</div>
</body>
</html>
